package io.github.iogogogo;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Maps;
import io.github.iogogogo.commons.util.DateTimeUtils;
import io.github.iogogogo.commons.util.IdHelper;
import io.github.iogogogo.commons.util.JsonParse;
import io.github.iogogogo.redis.RedisHandler;
import io.github.iogogogo.redis.properties.RedisInstanceProperties;
import io.github.iogogogo.redis.util.RedisTemplateUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;

import java.util.Map;

/**
 * Created by tao.zeng on 2024/11/16.
 */
@Slf4j
@SpringBootApplication(exclude = RedisAutoConfiguration.class)
public class MultipleRedisApplication implements CommandLineRunner {

    @Resource
    private RedisHandler<String, String> redisHandler;

    @Resource
    private RedisInstanceProperties redisInstanceProperties;

    public static void main(String[] args) {
        SpringApplication.run(MultipleRedisApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {


        Map<String, Object> map = Maps.newHashMap();
        map.put("timestamp", DateTimeUtils.nowDateTime());
        map.put("id", IdHelper.id());
        map.put("username", "张三");

        RedisTemplateUtils<String, String> bizRedisUtils = redisHandler.getBizRedisTemplate();
        bizRedisUtils.set("key", JsonParse.tryToJson(map));

        bizRedisUtils.hashOperations().put("note:common:user", "1", JsonParse.tryToJson(map));
        bizRedisUtils.hashOperations().put("note:common:user", "2", JsonParse.tryToJson(map));
        bizRedisUtils.hashOperations().put("note:common:user", "3", JsonParse.tryToJson(map));


        log.info("{}", bizRedisUtils.get("key"));

        log.info("{}", redisHandler.getGlobalRedisTemplate().get("key", new TypeReference<Map<String, Object>>() {
        }));
    }
}